package org.example.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.example.entity.Goods;
import org.example.mapper.GoodsMapper;
import org.example.model.PageData;
import org.example.service.IGoodsService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class GoodsServiceImpl implements IGoodsService {

    private GoodsMapper goodsMapper;

    @Value("${pic.server}")
    private String picService;

    public GoodsServiceImpl(GoodsMapper goodsMapper) {
        this.goodsMapper = goodsMapper;
    }

    @Override
    public PageData<Goods> selectPageData(Integer page, Integer limit) {
        Page<Goods> goodsPage = PageHelper.startPage(page, limit);
        goodsMapper.selectPageData();

        PageData<Goods> pageData = new PageData<>();
        pageData.setTotal(goodsPage.getTotal());
        pageData.setCurrentPage(goodsPage.getPageNum());

        List<Goods> goodsList = goodsPage.getResult();

        goodsList.forEach(g -> g.setPic(picService + g.getPic()));

        pageData.setData(goodsList);

        return pageData;
    }
}
